package com.fr.lintcode;

public class Q600 {

    /**
     * @param image: a binary matrix with '0' and '1'
     * @param x: the location of one of the black pixels
     * @param y: the location of one of the black pixels
     * @return: an integer
     */
    public int minArea(char[][] image, int x, int y) {
        // write your code here
    	if(image == null || image.length == 0 || image[0].length == 0) {
    		return 0;
    	}
    	int minX = Integer.MAX_VALUE;
    	int minY = Integer.MAX_VALUE;
    	int maxX = 0;
    	int maxY = 0;
    	for(int i =0; i < image.length; i++) {
    		for(int j = 0; j < image[i].length; j ++) {
    			if('1' == image[i][j]) {
    				if(minX > j) {
    					minX = j;
    				}
    				if(minY > i) {
    					minY = i;
    				}
    				if(maxX < j) {
    					maxX = j;
    				}
    				if(maxY < i) {
    					maxY = i;
    				}
    			}
    		}
    	}
    	if(minX > maxX || minY > maxY) {
    		return 0;
    	}
    	return(maxX - minX + 1) * (maxY - minY + 1);
    }

}
